<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序" prop="sortNo">
				<el-input-number v-model="form.sortNo" :min="1"></el-input-number>
			</el-form-item>
			<el-form-item label="文件夹名称" prop="boxName">
				<el-input v-model="form.boxName" placeholder="文件夹名称" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		components: {
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					boxId:"",
					sortNo: 1,
					boxName:"",
				},
				//验证规则
				rules: {
					sortNo: [
						{required: true, message: '排序号不能为空！'}
					],
					boxName: [
						{required: true, message: '文件夹名称不能为空！'}
					]
				}
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				if(this.mode=="add")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.email.insertEmailBox.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('updateemailbox')
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}else if(this.mode=="edit")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.email.updateEmailBox.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.visible = false;
								this.$emit('updateemailbox')
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}
			},
			setData(data){
				this.form.boxId = data.boxId
				this.form.sortNo = data.sortNo
				this.form.boxName = data.boxName
			}
		}
	}
</script>

<style>
</style>
